/* Name: LogFile
 * Author: Tom Dickman
 * Date: 5-8-12
 * Description: Used to write text a logfile with time and date stamps
 * Revisions: See Subversion logs at http://code.google.com/p/software-engineering-2012/source/list
 */

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Timestamp;

public class LogFile {
	private String fileName;
	private BufferedWriter out;
	
	// Creates log file at specified location if doesn't exist - Appends if already exists
	public LogFile(String theFileName) {
		fileName = theFileName;
		try { // Create a new log file, and clear it if it already exists
			FileWriter outFile = new FileWriter(fileName,true);
			out = new BufferedWriter(outFile);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	// Appends the given text to the log file, with a timestamp before each line
	public void append(String text) {
		try {
			out.write(getTimestamp() + " " + text);
			out.newLine();
			out.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	// Returns the timestamp as a string
	private String getTimestamp() {
		java.util.Date date= new java.util.Date();
		java.sql.Timestamp ts1 = new Timestamp(date.getTime());
		return ts1.toString();
	}
}
